// To change this License template, choose Tools / Templates
// and edit Licenses / FanDefaultLicense.txt
//
// History:
//   09.04.2011 jstaudemeyer Creation
//

using [java]javax.sound.midi::Sequence as JavaSequence
using [java]javax.sound.midi::Track as JavaTrack
using [java]javax.sound.midi::MidiEvent as JavaEvent
using [java]javax.sound.midi::MidiMessage as JavaMessage
using [java]javax.sound.midi::MetaMessage as JavaMetaMessage
using [java]javax.sound.midi::ShortMessage as JavaShortMessage
using [java]javax.sound.midi::SysexMessage as JavaSysexMessage
using [java]fanx.interop::ByteArray


**
** MidiSequence
**
class MidiSequence
{
  private JavaSequence javaSequence
  
  private MidiTrack[] trackList
  
  new make(Int resolution, Int numTracks:=0) {
    javaSequence = JavaSequence(Sequence.PPQ, resolution)
    numTracks.times |Int n| { createTrack }
  }
  
  MidiTrack createTrack() {
    newTrack := MidiTrack(javaSequence.createTrack())
    trackList.add(newTrack)
    return newTrack
  }
    
}

class MidiTrack
{
  internal JavaTrack javaTrack
  
  internal new make(JavaTrack javaTrack) {
    this.javaTrack = javaTrack
  }
  
  Bool add(MidiEvent event) {
    javaTrack.add(event.javaEvent)
  }
  
}

class MidiEvent {
  
  internal JavaEvent javaEvent
    
  new make(Int tick, MidiMessage midiMessage) {
    javaEvent = JavaEvent(midiMessage.javaMessage)
    javaEvent.setTick(tick)
  }
    
}

class MidiMessage {
  
  internal JavaMessage javaMessage
  
  new make (Int[] data) {
    javaMessage = JavaMessage(makeBytes(data))
  }
  
  protected new makeIntern(JavaMessage javaMessage) {
    this.javaMessage = javaMessage
  }
  
  private static ByteArray makeBytes (Int[] array) {
    size := array.size
    bytes := ByteArray(size)
    for (i:=0; i<size; i++) { bytes[i] = array[i] }
    return bytes
  }
  
}

class MetaMessage : MidiMessage {
  
  new make(Int[] array) : super.makeIntern(JavaMetaMessage(makeBytes(array))) {}
  
}

class ShortMessage : MidiMessage {
  new make(Int status, Int data1:=0, Int data2:=0) {
    
  }
}





